const UserDao = require("../dao/user-dao.js");
const bcrypt = require("bcrypt");
const UserService = {
	login(req,res,next){
		const {username,password} = req.body;
		console.log({username, password})
		UserDao.find({username})
		.then((data)=>{
			console.log(data);
			if(data.length===1){
				if(data[0].password===password){
					res.json({
						res_code:1,
						res_error:"",
						res_body:{
							status:1,
							data:{
								username
							}
						}
					});
				}else{
					res.json({
						res_code:1,
						res_error:"",
						res_body:{
							status:0,
							data:{}
						}
					});
				}
			}else{
				res.json({
					res_code:1,
					res_error:"",
					res_body:{
						status:0,
						data:{}
					}
				});
			}
			console.log(data);
		})
		.catch((err)=>{
			console.log(err);
			res.json({
				res_code:0,
				res_error:err,
				res_body:{}
			});
		});
		//从数据库中查询相关的信息
		//如果存在用户信息，则比较账户密码是否一直
	},
	register(req,res,next){
		const {username,password,email} = req.body;
		var hash = bcrypt.hashSync(password, 10);
		UserDao.save({username,password:hash,email})
			.then((data)=>{
				console.log(data);
				res.json({
					res_code:1,
					res_error:"",
					res_body:{
						status:1,
						data:{
							username
						}
					}
				});
			})
			.catch((err)=>{
				res.json({
					res_code:0,
					res_error:err,
					res_body:{}
				});
			});
	},
	logout(){}
};
module.exports = UserService;